﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class campaignTemplatesOutput : Api_BaseOutput
   {
      public List<campaignTemplatesResults> result { get; set; }
      public campaignTemplatesParms inputParms { get; private set; }
      // --------------------------------------------------------
      public campaignTemplatesOutput( campaignTemplatesInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new List<campaignTemplatesResults>();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "Campaign templates: {0}", this.result.Count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }

      // ---- xmlrpc
      internal void Format( x_campaignTemplates[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      private void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            int i;
            IEnumerable<XElement> templates = xml.Elements( "struct" );
            foreach ( XElement template in templates )
            {
               campaignTemplatesResults results = new campaignTemplatesResults();
               int.TryParse( template.Element( "id" ).Value, out i );
               results.id = i;
               results.name = template.Element( "name" ).Value ?? string.Empty;
               results.layout = template.Element( "layout" ).Value ?? string.Empty;
               results.preview_image = template.Element( "preview_image" ).Value ?? string.Empty;
               if ( template.Element( "sections" ).HasElements )
               {
                  foreach ( XElement section in template.Element( "sections" ).Descendants() )
                  {
                     results.sections.Add( section.Value );
                  }
               }
               this.result.Add( results );
            }
         }
         // ----------------------------- error
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      private void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_campaignTemplates[] jsonResult = jss.Deserialize<x_campaignTemplates[]>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- FormatResults
      protected List<campaignTemplatesResults> FormatResults( x_campaignTemplates[] x_results )
      {
         List<campaignTemplatesResults> results = new List<campaignTemplatesResults>();
         //
         foreach ( x_campaignTemplates xt in x_results )
         {
            campaignTemplatesResults template = new campaignTemplatesResults();
            template.id = Convert.ToInt32( xt.id );
            template.name = xt.name ?? string.Empty;
            template.layout = xt.layout ?? string.Empty;
            template.preview_image = xt.preview_image ?? string.Empty;
            template.sections = apiHelper.MakeListFromArray<string>( xt.sections );

            results.Add( template );
         }
         //
         return results;
      }

      #endregion
   }
}
